package com.mall.server.controller.sms;

import com.mall.model.domain.SmsHomeAdvertise;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.CommonResult;
import com.mall.server.service.sms.SmsHomeAdvertiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/home/advertise")
public class SmsHomeAdvertiseController {

    @Autowired
    private SmsHomeAdvertiseService smsHomeAdvertiseService;

    /**
     * 分页查询广告
     */
    @GetMapping("/list")
    public CommonResult list(@RequestParam(defaultValue = "1") Integer pageNum
            , @RequestParam(defaultValue = "10") Integer pageSize
            , String name, String endTime, Integer type) {

        CommonPage cp = smsHomeAdvertiseService.list(pageNum, pageSize, name, endTime, type);

        return CommonResult.success(cp);
    }

    /**
     * 添加广告
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody SmsHomeAdvertise advertise) {

        smsHomeAdvertiseService.create(advertise);

        return CommonResult.success(null);
    }

    /**
     * 删除广告
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {

        smsHomeAdvertiseService.delete(ids);

        return CommonResult.success(null);
    }

    /**
     * 修改上下线状态
     */
    @PostMapping("/update/status/{id}")
    public CommonResult updateStatus(@PathVariable Integer id,
                                     SmsHomeAdvertise advertise) {

        smsHomeAdvertiseService.update(id, advertise);

        return CommonResult.success(null);
    }

    /**
     * 修改广告
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Integer id,
                               @RequestBody SmsHomeAdvertise advertise) {

        smsHomeAdvertiseService.update(id, advertise);

        return CommonResult.success(null);
    }

    /**
     * 获取广告详情
     */
    @GetMapping("/{id}")
    public CommonResult findById(@PathVariable Integer id) {

        SmsHomeAdvertise advertise = smsHomeAdvertiseService.findById(id);

        return CommonResult.success(advertise);
    }

}
